package com.example.javaproject.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.javaproject.entity.PositionApplication;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface PositionApplicationMapper extends BaseMapper<PositionApplication> {
    /**
     * 查询某教师所有岗位的学生申请及相关信息
     */
    @Select("SELECT pa.id, p.name AS positionName, s.name AS studentName, pa.reason, pa.schedule, pa.status FROM position_application pa JOIN position p ON pa.position_id = p.id JOIN student s ON pa.student_id = s.id WHERE p.teacher_id = #{teacherId} AND pa.is_deleted = 0 AND s.is_deleted = 0 ORDER BY pa.create_time DESC")
    List<Map<String, Object>> selectApplicationsByTeacherId(@Param("teacherId") Integer teacherId);
    
    /**
     * 查询学生的申请记录（分页）
     */
    @Select("<script>" +
            "SELECT pa.id, p.name AS positionName, p.description, p.location, p.salary, p.time AS workTime, " +
            "pa.reason, pa.schedule, pa.status, pa.suggestion, pa.create_time AS createTime " +
            "FROM position_application pa " +
            "JOIN position p ON pa.position_id = p.id " +
            "WHERE pa.student_id = #{studentId} AND pa.is_deleted = 0 " +
            "<if test='positionName != null and positionName != \"\"'>" +
            "AND p.name LIKE CONCAT('%', #{positionName}, '%') " +
            "</if>" +
            "<if test='status != null'>" +
            "AND pa.status = #{status} " +
            "</if>" +
            "ORDER BY pa.create_time DESC " +
            "LIMIT #{offset}, #{size}" +
            "</script>")
    List<Map<String, Object>> selectMyApplicationsList(@Param("studentId") Integer studentId,
                                                       @Param("positionName") String positionName,
                                                       @Param("status") Integer status,
                                                       @Param("offset") Integer offset,
                                                       @Param("size") Integer size);
    
    /**
     * 查询学生的申请记录总数
     */
    @Select("<script>" +
            "SELECT COUNT(*) " +
            "FROM position_application pa " +
            "JOIN position p ON pa.position_id = p.id " +
            "WHERE pa.student_id = #{studentId} AND pa.is_deleted = 0 " +
            "<if test='positionName != null and positionName != \"\"'>" +
            "AND p.name LIKE CONCAT('%', #{positionName}, '%') " +
            "</if>" +
            "<if test='status != null'>" +
            "AND pa.status = #{status} " +
            "</if>" +
            "</script>")
    Integer selectMyApplicationsCount(@Param("studentId") Integer studentId,
                                     @Param("positionName") String positionName,
                                     @Param("status") Integer status);
    
    /**
     * 查询学生的申请记录（包含分页信息）
     */
    default Map<String, Object> selectMyApplications(Map<String, Object> params) {
        Integer studentId = (Integer) params.get("studentId");
        String positionName = (String) params.get("positionName");
        Integer status = (Integer) params.get("status");
        Integer page = (Integer) params.get("page");
        Integer size = (Integer) params.get("size");
        
        // 计算偏移量
        Integer offset = (page - 1) * size;
        
        // 查询数据列表
        List<Map<String, Object>> records = selectMyApplicationsList(studentId, positionName, status, offset, size);
        
        // 查询总数
        Integer total = selectMyApplicationsCount(studentId, positionName, status);
        
        // 构建返回结果
        Map<String, Object> result = new java.util.HashMap<>();
        result.put("records", records);
        result.put("total", total);
        result.put("current", page);
        result.put("size", size);
        
        return result;
    }

    /**
     * 教师分页查询所有岗位的学生申请及相关信息
     */
    @Select("<script>"
            + "SELECT pa.id, p.name AS positionName, s.name AS studentName, pa.reason, pa.schedule, pa.status "
            + "FROM position_application pa "
            + "JOIN position p ON pa.position_id = p.id "
            + "JOIN student s ON pa.student_id = s.id "
            + "WHERE p.teacher_id = #{teacherId} AND pa.is_deleted = 0 AND s.is_deleted = 0 "
            + "ORDER BY CASE pa.status WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 2 ELSE 3 END, pa.create_time DESC "
            + "LIMIT #{offset}, #{size}"
            + "</script>")
    List<Map<String, Object>> selectApplicationsByTeacherIdPagedList(@Param("teacherId") Integer teacherId,
                                                                    @Param("offset") Integer offset,
                                                                    @Param("size") Integer size);

    @Select("<script>"
            + "SELECT COUNT(*) "
            + "FROM position_application pa "
            + "JOIN position p ON pa.position_id = p.id "
            + "JOIN student s ON pa.student_id = s.id "
            + "WHERE p.teacher_id = #{teacherId} AND pa.is_deleted = 0 AND s.is_deleted = 0 "
            + "</script>")
    Integer selectApplicationsByTeacherIdCount(@Param("teacherId") Integer teacherId);

    default Map<String, Object> selectApplicationsByTeacherIdPaged(Map<String, Object> params) {
        Integer teacherId = (Integer) params.get("teacherId");
        Integer page = (Integer) params.get("page");
        Integer size = (Integer) params.get("size");
        Integer offset = (page - 1) * size;
        List<Map<String, Object>> records = selectApplicationsByTeacherIdPagedList(teacherId, offset, size);
        Integer total = selectApplicationsByTeacherIdCount(teacherId);
        Map<String, Object> result = new java.util.HashMap<>();
        result.put("records", records);
        result.put("total", total);
        result.put("current", page);
        result.put("size", size);
        return result;
    }
} 